package cn.ffcs.util;

import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;
/**
 * 
 * 字典类型DAO实现<br/> <table>
 * <tr>
 * <td>负责人/principal: </td>
 * <td colspan="2">liuzhh</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author g.huangjx
 * @version $Id$
 */
public class GetSet
{
    /*
     * 获取对像的方法
     */
    public Object getMethod(Object obj, String method, String[] args)
            throws Exception {
        Object result = null;
        Method meth = null;
        Method[] methods = obj.getClass().getMethods();
        for (int i = 0; i < methods.length; i++) {//遍历类的方法名
            if (methods[i].getName().equals(method)) {
                meth = methods[i];//绑定方法名
                break;
            }
        }
        if (meth == null){
            //System.out.println("类："+obj.getClass().getName()+" 不存在方法名 "+method);
            return null;
        }
        String[] paramType = getType(meth);//获取参数类型
        Object[] paramObj = getMethodObject(paramType, args);//获取方法参数对应值
        result = meth.invoke(obj, paramObj); // 绑定对象执行get函数
        if (result == null) {
            return null;
        }
        return result;
    }
//  获取参数 Object[]的方法
    public Object[] getMethodObject(String[] type, String[] param) {
        Object[] obj = new Object[param.length];
        for (int i = 0; i < obj.length; i++) {
            if (!param[i].trim().equals("") || param[i] != null) {
                if(type[i].endsWith("String")){
                    obj[i]=param[i];
                } else if (type[i].equals("int") || type[i].endsWith("Integer")) {
                    obj[i] = new Integer(param[i]);
                } else if (type[i].equals("long") || type[i].endsWith("Long")) {
                    obj[i] = new Long(param[i]);
                } else if (type[i].equals("float") || type[i].endsWith("Float")) {
                    obj[i] = new Float(param[i]);
                } else if (type[i].equals("double") || type[i].endsWith("Double")) {
                    obj[i] = new Double(param[i]);
                
                } else if (type[i].equals("boolean")
                        || type[i].equals("Boolean")) {
                    obj[i] = new Boolean(param[i]);
                } else {
                    obj[i] = param[i];
                }
            }
        }
        return obj;
    }
    /*
     * 获取方法参数的类型
     */
    public String[] getType(Method method) {
        Class[] parameterTypes = method.getParameterTypes();
        int len = parameterTypes.length;
        String[] type = new String[len];//定义方法参数
        for (int j = 0; j < len; j++) {
            type[j] = parameterTypes[j].toString();
        }
        return type;
    }
    public static void main(String [] a){
        String [] args={"1","abc"};
        GetSet getSet=new GetSet();
        try{
            Object returnObject = getSet.getMethod(Class.forName("cn.ffcs.util.Test").newInstance(),"getTest", args);
           System.out.println("Object returnObject="+returnObject.toString());
           String [] aa={"1","2.2","3","我是中国人"};
           List ls=(List) getSet.getMethod(Class.forName("cn.ffcs.util.Test").newInstance(),"getList", aa);
           Iterator it=ls.iterator();
           while(it.hasNext()){
               System.out.println(it.next().toString());
           }
           
        }catch(Exception e){
            e.printStackTrace();
        }
    } 
}
